package com.googlecode.bjpodcast.data;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;

public class PodcastDBProcess {
	private static final String TAG = PodcastDBProcess.class.getName();
	private PodcastDbAdapter podcastDb;
	private Context con;

	public PodcastDBProcess(Context c) {
		this.con = c;
		podcastDb = new PodcastDbAdapter(con);

	}

	// add Feed (Channel + Item)
	public void addChannel(Channel channel) throws Exception {

		try {
			podcastDb.open();

			// add channel
			long channelID = podcastDb.createChannel(channel);
			if(channelID != -1) {
				// add items
				for (Item item : channel.getItems()) {
					podcastDb.addItem(channelID, item);
				}
			}
			

			podcastDb.close();
			
		} catch (Exception e) {
			Log.e(TAG, "addChannel error", e);
		} finally {
			if(podcastDb != null) {
				podcastDb.close();
			}
		}
	}
	
	// fetch All Channel
	public Cursor getChannelList() throws Exception {
		Cursor cursor = null;;
		try{
			podcastDb.open();
			cursor = podcastDb.fetchAllChannel();
		} catch (Exception e) {
			Log.e(TAG, "getChannelList error", e);
		}finally {
			if(podcastDb != null) {
				podcastDb.close();
			}
		}
		return cursor;
		
	}
}

